package com.avic.rong.fragment;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.avic.rong.BaseFragment;
import com.avic.rong.R;
import com.avic.rong.adapter.MyRecycleViewAdapter;
import com.avic.rong.api.HomeApi;
import com.avic.rong.bean.HomeBean;
import com.avic.rong.goodsSort.activity.GoodsSortActivity;
import com.avic.rong.wedgit.customWedgit.NavigatorPopup;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import butterknife.BindView;
import butterknife.OnClick;


public class FragmentShop extends BaseFragment {


    public static final String ARG_COME_HOME = "come_home";

    @BindView(R.id.pull_recycler_view_home)
    RecyclerView recycler_view_home;
    @BindView(R.id.imag_rr)
    ImageView imagReturn;
    @BindView(R.id.shop_fenlei)
    ImageView textViewSearchCancel;
    @BindView(R.id.shop_search_editText)
    EditText editTextSearch;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;


    private HomeBean homeBean;
    private HomeApi homeApi;

    private MyRecycleViewAdapter recycleViewAdapter;

    public static FragmentShop newInstance(String param1, String param2) {
        FragmentShop fragment = new FragmentShop();
        return fragment;
    }


    @Override
    protected int getContentViewId() {
        return R.layout.fragment_fragment_shop;
    }


   /* @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_fragment_shop, container, false);
        ButterKnife.bind(this, rootView);


        homeApi = new HomeApi((RxAppCompatActivity) getActivity(), listener);
        HttpManager.getInstance().doHttpDeal(homeApi);

        recycleViewAdapter = new MyRecycleViewAdapter(getActivity());
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        recycler_view_home.setLayoutManager(layoutManager);
        recycler_view_home.setAdapter(recycleViewAdapter);
        recycler_view_home.setItemAnimator(new DefaultItemAnimator());


        recycler_view_home.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                if (!recycler_view_home.canScrollVertically(-1)) {//到顶部
                    if (homeBean == null || homeBean.getAdvertismentList() == null || homeBean.getAdvertismentList().size() <= 0) {
                        HttpManager.getInstance().doHttpDeal(homeApi);
                    }
                }
            }
        });

        textViewSearchCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), GoodsSortActivity.class);
                getActivity().startActivity(intent);
            }
        });


        return rootView;
    }*/

    //去搜索主页
    @OnClick(R.id.shop_search_editText)
    public void goSearchIndex() {
//        startActivity(SearchIndexActivity.queryIntent(getActivity(), AppUtils.BASE_URL + "/app/searchIndex", ARG_COME_HOME));
    }

    //    @OnClick(R.id.gengduo)
    public void goGengdo() {
        NavigatorPopup filterAttrPopup = new NavigatorPopup(getActivity());
//        filterAttrPopup.showPricePopup(gengduo);
//        Toast.makeText(getActivity(),"asd",Toast.LENGTH_SHORT).show();
//        startActivity(SearchIndexActivity.queryIntent(getActivity(),AppUtils.BASE_URL + "/app/searchIndex",ARG_COME_HOME));
    }


    private HttpOnNextListener listener = new HttpOnNextListener<BaseResultEntity>() {

        @Override
        public void onNext(BaseResultEntity entity) {
            homeBean = (HomeBean) entity.getData();
            recycleViewAdapter.setHomeBean((HomeBean) entity.getData());
            System.out.println("-----------------------" + entity.getMsg());
        }
    };

    @Override
    public void onStop() {
        super.onStop();
        if (recycleViewAdapter != null) {
            recycleViewAdapter.stopVerticalTextView();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    protected void initView() {
//        View rootView = inflater.inflate(R.layout.fragment_fragment_shop, container, false);
//        ButterKnife.bind(this, rootView);


        homeApi = new HomeApi((RxAppCompatActivity) getActivity(), listener);
        HttpManager.getInstance().doHttpDeal(homeApi);

        recycleViewAdapter = new MyRecycleViewAdapter(getActivity());
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        recycler_view_home.setLayoutManager(layoutManager);
        recycler_view_home.setAdapter(recycleViewAdapter);
        recycler_view_home.setItemAnimator(new DefaultItemAnimator());





//        return rootView;
    }

    @Override
    protected void addListener() {
        recycler_view_home.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                if (!recycler_view_home.canScrollVertically(-1)) {//到顶部
                    if (homeBean == null || homeBean.getAdvertismentList() == null || homeBean.getAdvertismentList().size() <= 0) {
                        HttpManager.getInstance().doHttpDeal(homeApi);
                    }
                }
            }
        });

        textViewSearchCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), GoodsSortActivity.class);
                getActivity().startActivity(intent);
            }
        });
    }

}
